//
//  TabBar.swift
//  weibo
//
//  Created by 罗心 on 16/6/25.
//  Copyright © 2016年 罗心. All rights reserved.
//

import UIKit
//代理协议
protocol LXTabBarDelegate: NSObjectProtocol {
    func composeButtonClick()
    
}

class LXTabBar: UITabBar {
    
    //声明代理
    weak var lxDelegate: LXTabBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = ScreenW / 5
        var index: CGFloat = 0
        for value in self.subviews{
            if value .isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.frame.size.width = width
                value.frame.origin.x = index * width
                ++index
                if index == 2 {
                    ++index
                }
                
            }
        }
        composeButton.center.x = self.frame.size.width * 0.5
        composeButton.center.y = self.frame.size.height * 0.5
        
    }
    
    func setupUI() {
        addSubview(composeButton)
    }
    
    //监听方法
   @objc private func buttonClick(){
        lxDelegate?.composeButtonClick()
    }
    
    
    //撰写按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //添加点击事件
        button.addTarget(self, action: #selector(LXTabBar.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        button.sizeToFit()
       
        return button
    }()
    
    
    
    
    
    
    
    

}
